<template>
  <div class="content">
    <div class="category">
      <!-- category__item--active -->

      <div
        class="category__item"
        :class="item.tab === currentTab ? 'category__item--active' : ''"
        v-for="item in categories"
        :key="item.name"
        @click="handleTabClick(item.tab)"
      >
        {{ item.name }}
      </div>
    </div>
    <div class="product">
      <div class="product__item" v-for="item in list" :key="item._id">
        <img class="product__item__img" :src="item.imgUrl" alt="" />
        <div class="product__item__detail">
          <h4 class="product__item__title">
            {{ item.name }}
          </h4>
          <p class="product__item__sales">月售{{ item.sales }}件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;</span>{{ item.price }}
            <span class="product__item__origin">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__number">
          <span
            class="product__number__minus"
            @click="
              () => {
                changeCartItem(shopId, item._id, item, -1, shopName);
              }
            "
          >
            <span class="move">-</span>
          </span>
          <!-- {{ cartList?.[shopId]?.productList?.[item._id]?.count || 0 }} -->
          {{ getProductCartCount(shopId, item._id) }}
          <span
            class="product__number__plus"
            @click="
              () => {
                changeCartItem(shopId, item._id, item, 1, shopName);
              }
            "
            >+</span
          >
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { reactive, toRefs, ref } from '@vue/reactivity';
import { get } from '@/utils/request';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import { watchEffect } from '@vue/runtime-core';
import { useCommonCartEffect } from '@/effects/cartEffects';
const categories = [
  { name: '全部商品', tab: 'all' },
  { name: '秒杀', tab: 'seckill' },
  { name: '新鲜水果', tab: 'fruit' },
];
// tab切换相关逻辑
const useTabEffect = () => {
  const currentTab = ref(categories[0].tab);
  const handleTabClick = (tab) => {
    currentTab.value = tab;
  };
  return { currentTab, handleTabClick };
};

// 列表内容相关逻辑
const useCurrentListEffect = (currentTab, shopId) => {
  const content = reactive({
    list: [],
  });
  const getContentData = async () => {
    const result = await get(`/api/shop/${shopId}/products`, {
      tab: currentTab.value,
    });
    if (result?.errno === 0 && result?.data?.length) {
      content.list = result.data;
    }
  };
  watchEffect(() => {
    getContentData();
  });
  const { list } = toRefs(content);
  return { list };
};

// 购物车相关逻辑
const useCartEffect = (shopId) => {
  const store = useStore();
  const { changeCartItemInfo } = useCommonCartEffect(shopId);
  const cartList = store.state.cartList;

  const changeShopName = (shopId, shopName) => {
    store.commit('changeShopName', { shopId, shopName });
  };
  const changeCartItem = (shopId, productId, productInfo, num, shopName) => {
    changeCartItemInfo(shopId, productId, productInfo, num);
    changeShopName(shopId, shopName);
  };
  const getProductCartCount = (shopId, productId) => {
    return cartList?.[shopId]?.productList?.[productId]?.count || 0;
  };
  return { cartList, changeShopName, changeCartItem, getProductCartCount };
};

export default {
  name: 'Content',
  props: ['shopName'],
  setup() {
    const route = useRoute();
    const shopId = route.params.id;
    const { currentTab, handleTabClick } = useTabEffect();
    const { list } = useCurrentListEffect(currentTab, shopId);
    const { cartList, changeCartItem, getProductCartCount } =
      useCartEffect(shopId);
    return {
      list,
      currentTab,
      categories,
      handleTabClick,
      cartList,
      shopId,
      changeCartItem,
      getProductCartCount,
    };
  },
};
</script>
<style lang="scss" scoped>
@import '@/style/viriables.scss';
@import '@/style/mixins.scss';
.content {
  display: flex;
  position: absolute;
  // background-color: rgb(179, 170, 170);
  top: 1.6rem;
  left: 0;
  right: 0;
  bottom: 0.5rem;
  .category {
    overflow-y: scroll;
    width: 0.76rem;
    background-color: $search-bgColor;
    &__item {
      line-height: 0.4rem;
      text-align: center;
      font-size: 0.14rem;
      &--active {
        background-color: $bgColor;
      }
    }
  }
  .product {
    overflow-y: scroll;
    flex: 1;
    &__item {
      display: flex;
      padding: 0.12rem 0;
      margin: 0 0.16rem;
      border-bottom: 0.01rem solid $content-bgColor;
      position: relative;
      &__img {
        width: 0.68rem;
        height: 0.68rem;
        margin-right: 0.16rem;
      }
      &__detail {
        overflow: hidden;
      }
      &__title {
        margin: 0;
        line-height: 0.2rem;
        font-size: 0.14rem;
        color: $content-fontcolor;
        @include ellipsis;
      }
      &__sales {
        margin: 0.06rem 0;
        font-size: 0.12rem;
        color: $content-fontcolor;
      }
      &__price {
        margin: 0;
        line-height: 0.2rem;
        font-size: 0.14rem;
        color: $hightlight-fontColor;
      }
      &__yen {
        font-size: 0.12rem;
      }
      &__origin {
        margin-left: 0.06rem;
        line-height: 0.2rem;
        font-size: 0.12rem;
        color: $light-fontColor;
        text-decoration: line-through;
      }
    }
    &__number {
      position: absolute;
      right: 0.18rem;
      bottom: 0.12rem;
      &__minus,
      &__plus {
        display: inline-block;
        width: 0.2rem;
        height: 0.2rem;
        border-radius: 50%;
        font-size: 0.2rem;
        text-align: center;
        line-height: 0.2rem;
      }
      &__minus {
        border: 0.01rem solid $medium-fontColor;
        color: $medium-fontColor;
        margin-right: 0.05rem;
      }
      &__plus {
        background: $btn-bgColor;
        color: $bgColor;
        margin-left: 0.05rem;
      }
    }
  }
}
.move {
  position: relative;
  top: -0.01rem;
}
</style>